package com.company.jrlz.service.opcua.config;

import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription;
import org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscriptionManager;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.builtin.DateTime;
import org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;

/**
 * Created by ziye on 2023/9/6
 */
public class SubscriptionListenerImpl implements UaSubscriptionManager.SubscriptionListener {

    private final Logger log = LoggerFactory.getLogger(SubscriptionListenerImpl.class);

    private OpcUaClientTemplate opcUaClientTemplate;

    public SubscriptionListenerImpl(OpcUaClientTemplate opcUaClientTemplate) {

        this.opcUaClientTemplate = opcUaClientTemplate;

    }


    @Override
    public void onSubscriptionTransferFailed(final UaSubscription subscription, final StatusCode statusCode) {
        log.info("订阅问题：订阅节点失败重新添加订阅onSubscriptionTransferFailed {} : {}", subscription.getSubscriptionId(), statusCode);

        opcUaClientTemplate.getOpcUaClient().getSubscriptionManager().removeSubscriptionListener(this);
        opcUaClientTemplate.getOpcUaClient().getSubscriptionManager().clearSubscriptions();
        try {
            opcUaClientTemplate.getOpcUaClient().disconnect().get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        if(null == opcUaClientTemplate.getItemsAlive() || opcUaClientTemplate.getItemsAlive().size() == 0 ){
            opcUaClientTemplate.fireConnectionListeners();
            log.info("getItemsAlive null");
        }else {
            subscription.deleteMonitoredItems(opcUaClientTemplate.getItemsAlive());
            opcUaClientTemplate.fireConnectionListeners();
        }

//        }

        // we simply tear it down and build it up again
//        handleConnectionFailue(new RuntimeException("Subscription failed to reconnect"));
    }

    @Override
    public void onStatusChanged(final UaSubscription subscription, final StatusCode status) {
        log.info("订阅问题：onStatusChanged");

        if(status.isBad()){
            if(null == opcUaClientTemplate.getItemsAlive() || opcUaClientTemplate.getItemsAlive().size() == 0 ){
                log.info("getItemsAlive null");
                opcUaClientTemplate.fireConnectionListeners();
            }else {
                subscription.deleteMonitoredItems(opcUaClientTemplate.getItemsAlive());
                subscription.deleteMonitoredItems(opcUaClientTemplate.getItemsAlive());
                opcUaClientTemplate.fireConnectionListeners();
            }
        }
//        log.info("Subscription status changed {} : {}", subscription.getSubscriptionId(), status);
    }

    @Override
    public void onPublishFailure(final UaException exception) {
//        log.info("订阅问题：onPublishFailure");
    }

    @Override
    public void onNotificationDataLost(final UaSubscription subscription) {
        log.info("订阅问题：onNotificationDataLost数据丢失");

    }

    @Override
    public void onKeepAlive(final UaSubscription subscription, final DateTime publishTime) {
//        log.info("订阅问题：onKeepAlive");
    }

}
